# Copyright 2019-2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

project(ascendcl_utest)

set(CMAKE_CXX_STANDARD 11)

if(DEFINED ENV{ASCEND_CUSTOM_PATH})
    set(ASCEND_DIR $ENV{ASCEND_CUSTOM_PATH})
    set(ASCEND_OPENSDK_DIR ${ASCEND_DIR}/opensdk/opensdk)
    message("Ascend custom path is ${ASCEND_DIR}")
else()
    set(ASCEND_DIR /usr/local/Ascend)
endif()


#include directories
include_directories(${CMAKE_CURRENT_LIST_DIR})
include_directories(${BASE_DIR})
include_directories(${BASE_DIR}/inc)
include_directories(${BASE_DIR}/inc/external)
include_directories(${BASE_DIR}/third_party/inc)
include_directories(${ASCEND_OPENSDK_DIR}/include/runtime)
include_directories(${ASCEND_OPENSDK_DIR}/include/slog)
include_directories(${ASCEND_OPENSDK_DIR}/include/msprof)
include_directories(${ASCEND_OPENSDK_DIR}/include/ascend_hal)
include_directories(${ASCEND_OPENSDK_DIR}/include/datagw)
include_directories(${ASCEND_OPENSDK_DIR}/include/air)
include_directories(${ASCEND_OPENSDK_DIR}/include/air/framework)
include_directories(${ASCEND_OPENSDK_DIR}/include/air/external)
include_directories(${ASCEND_OPENSDK_DIR}/include/metadef)
include_directories(${ASCEND_OPENSDK_DIR}/include/metadef/external)
include_directories(${BASE_DIR}/common)
include_directories(${BASE_DIR}/single_op)
include_directories(${BASE_DIR}/toolchain)
include_directories(${BASE_DIR}/types)
include_directories(${BASE_DIR}/utils)
include_directories(${ASCEND_OPENSDK_DIR}/include/adump/external)
include_directories(${ASCEND_OPENSDK_DIR}/include)
include_directories(${ASCEND_OPENSDK_DIR}/include/tdt)
include_directories(${BASE_DIR}/third_party/patch/securec)
include_directories(${BASE_DIR}/tests/depends/)
include_directories(${CMAKE_BINARY_DIR})
include_directories(${BASE_DIR}/output/protobuf/include)
include_directories(${BASE_DIR}/third_party/inc/dvpp/inc/dvpp)
include_directories(${ASCEND_OPENSDK_DIR}/include/acldvpp/external/dvpp)

set(UT_FILES
    "testcase/compatibility/enum_check.cpp"
    "testcase/compatibility/const_check.cpp"
    "testcase/compatibility/struct_check.cpp"
    "testcase/acl_model_unittest.cpp"
    "testcase/acl_common_unittest.cpp"
    "testcase/acl_capability_unittest.cpp"
    "testcase/acl_toolchain_unittest.cpp"
    "testcase/acl_opCompile_unittest.cpp"
    "testcase/acl_opAttr_unittest.cpp"
    "testcase/acl_opExecutor_unittest.cpp"
    "testcase/acl_opModelCache_unittest.cpp"
    "testcase/acl_opModelParser_unittest.cpp"
    "testcase/acl_op_unittest.cpp"
    "testcase/acl_retrInternal_unittest.cpp"
    "testcase/acl_retrOps_unittest.cpp"
    "testcase/acl_retr_unittest.cpp"
    "testcase/acl_builtinOp_unittest.cpp"
    "testcase/acl_fp16_unittest.cpp"
    "testcase/acl_blas_unittest.cpp"
    "testcase/acl_vpc_unittest.cpp"
    "testcase/acl_dvpp_unittest.cpp"
    "testcase/acl_channel_unittest.cpp"
    "testcase/acl_jpeg_unittest.cpp"
    "testcase/acl_png_unittest.cpp"
    "testcase/acl_vdec_unittest.cpp"
    "testcase/acl_venc_unittest.cpp"
    "testcase/acl_runtime_unittest.cpp"
    "testcase/acl_tensorDataTransfer_unittest.cpp"
    "testcase/acl_queue_unittest.cpp"
    "testcase/acl_dvpp_himpi_unittest.cpp"
    "testcase/acl_resource_manager_unittest.cpp"
)

set(SRC_FILES
    "${BASE_DIR}/common/acl.cpp"
    "${BASE_DIR}/common/log_inner.cpp"
    "${BASE_DIR}/common/log.cpp"
    "${BASE_DIR}/common/json_parser.cpp"
    "${BASE_DIR}/common/error_codes_api.cpp"
    "${BASE_DIR}/runtime/device.cpp"
    "${BASE_DIR}/runtime/context.cpp"
    "${BASE_DIR}/runtime/stream.cpp"
    "${BASE_DIR}/runtime/memory.cpp"
    "${BASE_DIR}/runtime/event.cpp"
    "${BASE_DIR}/runtime/callback.cpp"
    "${BASE_DIR}/runtime/group.cpp"
    "${BASE_DIR}/runtime/allocator.cpp"
    "${BASE_DIR}/runtime/kernel.cpp"
    "${BASE_DIR}/model/model.cpp"
    "${BASE_DIR}/model/model_config.cpp"
    "${BASE_DIR}/model/acl_aipp.cpp"
    "${BASE_DIR}/model/aipp_param_check.cpp"
    "${BASE_DIR}/model/acl_resource_manager.cpp"
    "${BASE_DIR}/single_op/op.cpp"
    "${BASE_DIR}/single_op/op_executor.cpp"
    "${BASE_DIR}/single_op/op_model_cache.cpp"
    "${BASE_DIR}/single_op/op_model_parser.cpp"
    "${BASE_DIR}/single_op/ge_tensor_cache.cpp"
    "${BASE_DIR}/single_op/shape_range_utils.cpp"
    "${BASE_DIR}/toolchain/dump.cpp"
    "${BASE_DIR}/toolchain/profiling.cpp"
    "${BASE_DIR}/toolchain/profiling_manager.cpp"
    "${BASE_DIR}/toolchain/resource_statistics.cpp"
    "${BASE_DIR}/types/acl_op.cpp"
    "${BASE_DIR}/types/fp16.cpp"
    "${BASE_DIR}/types/fp16_impl.cpp"
    "${BASE_DIR}/types/op_attr.cpp"
    "${BASE_DIR}/types/op_model.cpp"
    "${BASE_DIR}/types/tensor_desc_internal.cpp"
    "${BASE_DIR}/utils/array_utils.cpp"
    "${BASE_DIR}/utils/attr_utils.cpp"
    "${BASE_DIR}/utils/math_utils.cpp"
    "${BASE_DIR}/utils/file_utils.cpp"
    "${BASE_DIR}/utils/hash_utils.cpp"
    "${BASE_DIR}/utils/cann_info_utils.cpp"
    "${BASE_DIR}/utils/string_utils.cpp"
    "${BASE_DIR}/single_op/blas/gemm_ops.cpp"
    "${BASE_DIR}/single_op/blas/gemv_ops.cpp"
    "${BASE_DIR}/single_op/builtin/cast_op.cpp"
    "${BASE_DIR}/single_op/compile/op_compiler.cpp"
    "${BASE_DIR}/single_op/op_compiler.cpp"
    "${BASE_DIR}/single_op/compile/local_compiler.cpp"
    "${BASE_DIR}/single_op/compile/op_compile_service.cpp"
    "${BASE_DIR}/single_op/compile/op_kernel_selector.cpp"
    "${BASE_DIR}/single_op/compile/op_kernel_registry.cpp"
    "${BASE_DIR}/single_op/compile/op_compile_processor.cpp"
    "${BASE_DIR}/single_op/executor/op_task.cpp"
    "${BASE_DIR}/single_op/executor/resource_manager.cpp"
    "${BASE_DIR}/single_op/executor/stream_executor.cpp"
    "${BASE_DIR}/single_op/dvpp/channel.cpp"
    "${BASE_DIR}/single_op/dvpp/jpeg.cpp"
    "${BASE_DIR}/single_op/dvpp/png.cpp"
    "${BASE_DIR}/single_op/dvpp/vpc.cpp"
    "${BASE_DIR}/single_op/dvpp/vdec.cpp"
    "${BASE_DIR}/single_op/dvpp/venc.cpp"
    "${BASE_DIR}/single_op/dvpp/base/image_processor.cpp"
    "${BASE_DIR}/single_op/dvpp/base/video_processor.cpp"
    "${BASE_DIR}/single_op/dvpp/mgr/dvpp_manager.cpp"
    "${BASE_DIR}/single_op/dvpp/v100/image_processor_v100.cpp"
    "${BASE_DIR}/single_op/dvpp/v200/image_processor_v200.cpp"
    "${BASE_DIR}/single_op/dvpp/v300/image_processor_v300.cpp"
    "${BASE_DIR}/single_op/dvpp/v100/video_processor_v100.cpp"
    "${BASE_DIR}/single_op/dvpp/v200/video_processor_v200.cpp"
    "${BASE_DIR}/single_op/dvpp/common/dvpp_util.cpp"
    "${BASE_DIR}/types/dvpp.cpp"
    "${BASE_DIR}/single_op/retr/retr_accurate.cpp"
    "${BASE_DIR}/single_op/retr/retr_init.cpp"
    "${BASE_DIR}/single_op/retr/retr_internal.cpp"
    "${BASE_DIR}/single_op/retr/retr_ops.cpp"
    "${BASE_DIR}/single_op/retr/retr_release.cpp"
    "${BASE_DIR}/single_op/retr/retr_repo.cpp"
    "${BASE_DIR}/single_op/retr/retr_search.cpp"
    "${BASE_DIR}/types/retr.cpp"
    "${BASE_DIR}/tensor_data_transfer/tensor_data_transfer.cpp"
    "${BASE_DIR}/tensor_data_transfer/queue.cpp"
    "${BASE_DIR}/tensor_data_transfer/queue_manager.cpp"
    "${BASE_DIR}/tensor_data_transfer/queue_process.cpp"
    "${BASE_DIR}/tensor_data_transfer/queue_process_host.cpp"
    "${BASE_DIR}/tensor_data_transfer/queue_process_mdc.cpp"
    "${BASE_DIR}/tensor_data_transfer/queue_process_ccpu.cpp"
)

############ libut_ascendcl_src.a ############
add_library(ut_ascendcl_src STATIC
    ${SRC_FILES}
)

target_compile_definitions(ut_ascendcl_src PRIVATE
    google=ascend_private
    ENABLE_DVPP_INTERFACE
    RUN_TEST
    $<$<STREQUAL:${ENABLE_OPEN_SRC},True>:ONLY_COMPILE_OPEN_SRC>
    $<$<STREQUAL:${ENABLE_ACL_UT},ON>:ONLY_ENABLE_ACL_UT>
)

target_compile_options(ut_ascendcl_src PRIVATE
    -g --coverage -fprofile-arcs -ftest-coverage
    -Wall -Wextra -Wfloat-equal -Werror
)

target_link_libraries(ut_ascendcl_src PRIVATE
    $<BUILD_INTERFACE:intf_pub>
    c_sec
    json
    ascend_protobuf
    static_turbojpeg
)

############ ascendcl_utest ############
add_executable(ascendcl_utest ${UT_FILES})

target_compile_options(ascendcl_utest PRIVATE
    -g
    -Wno-deprecated-declarations
    -Wall -Wfloat-equal -Werror
)

target_compile_definitions(ascendcl_utest PRIVATE
    google=ascend_private
    ENABLE_DVPP_INTERFACE
    ACL_BASE_DIR=\"${BASE_DIR}\"
)

target_link_libraries(ascendcl_utest PRIVATE
    $<BUILD_INTERFACE:intf_pub>
    ut_ascendcl_src
    json
    static_turbojpeg
    gtest
    gtest_main
    gmock
    gmock_main
    slog_stub
    c_sec
    mmpa_stub
    ge_stub
    tdt_stub
    toolchain_stub
    runtime_stub
    profiling_stub
    jpeg_stub
    himpi_stub
    -lrt
    -ldl
    -lpthread
    -lgcov
)
